package SMS;

import java.util.ArrayList;

import model.GroupContacts;
import android.app.Activity;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.database.Cursor;
import android.provider.ContactsContract;
import android.telephony.SmsManager;
import android.util.Log;
import android.widget.Toast;

public class SMSMessaging {
	
	Context context;
	
	public SMSMessaging(Context context) {
		this.context=context;
	}
	public ArrayList<String> getContactNumbers(ArrayList<GroupContacts> contactsList) {
		ArrayList<String> numbers=new ArrayList<String>();
		for(int i=0;i<contactsList.size();i++){
			String cntctName=contactsList.get(i).getContactName();
			Cursor people=context.getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
					new String[] {ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,
	                ContactsContract.CommonDataKinds.Phone.NUMBER}, 
	                null, null, null);
			int indexName = people.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME);
			int indexNumber = people.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
			people.moveToFirst();
			do {
			    if(people.getString(indexName).equalsIgnoreCase(cntctName)){
				    String number = people.getString(indexNumber);
				    numbers.add(number);
				    Log.d("name ph", cntctName+" "+number);
			    }
			} while (people.moveToNext());
		}
		return numbers;
	}

	public void sendMessage(ArrayList<String> phno, String msg) {
		
		Log.d("Sending message","Message="+msg);
		String SENT = "SMS_SENT";
        String DELIVERED = "SMS_DELIVERED";
                
		for(int i=0;i<phno.size();i++){
			String phoneNumber=phno.get(i).toString();
			Log.d("Sending message","phoneNumber="+phoneNumber);
			PendingIntent sentPI = PendingIntent.getBroadcast(context, 0,
		            new Intent(SENT), 0);
		 
		        PendingIntent deliveredPI = PendingIntent.getBroadcast(context, 0,
		            new Intent(DELIVERED), 0);
		 
		        //---when the SMS has been sent---
		        context.registerReceiver(new BroadcastReceiver(){
		            @Override
		            public void onReceive(Context arg0, Intent arg1) {
		            	switch (getResultCode())
		                {
		                    case Activity.RESULT_OK:
		                    	Toast.makeText(context,"SMS Sent", Toast.LENGTH_LONG).show();
		                        break;
		                    case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
		                    	 Toast.makeText(context,"Generic failure", Toast.LENGTH_LONG).show();
		                         break;
		                    case SmsManager.RESULT_ERROR_NO_SERVICE:
		                    	Toast.makeText(context,"No service", Toast.LENGTH_LONG).show();
		                        break;
		                    case SmsManager.RESULT_ERROR_NULL_PDU:
		                    	Toast.makeText(context,"Null PDU", Toast.LENGTH_LONG).show();
		                        break;
		                    case SmsManager.RESULT_ERROR_RADIO_OFF:
		                    	Toast.makeText(context,"Radio off", Toast.LENGTH_LONG).show();
		                        break;
		                }
		            }
		        }, new IntentFilter(SENT));
		 
		        //---when the SMS has been delivered---
		        context.registerReceiver(new BroadcastReceiver(){
		            @Override
		            public void onReceive(Context arg0, Intent arg1) {
		            	switch (getResultCode())
		                {
		                    case Activity.RESULT_OK:
			                    Toast.makeText(context,"SMS delivered", Toast.LENGTH_LONG).show();
			                    break;
		                    case Activity.RESULT_CANCELED:
		                    	Toast.makeText(context,"SMS not delivered", Toast.LENGTH_LONG).show();
		                        break;                        
		                }
		            }
		            
		        }, new IntentFilter(DELIVERED));  
		 
		        SmsManager sms = SmsManager.getDefault();
		        sms.sendTextMessage(phoneNumber, null, msg, sentPI, deliveredPI); 
		}
	}
	
}


